import ReplyItem from './ReplyItem';
import CommunicationRichText from './CommunicationRichText';
import isFunction from 'lodash/isFunction';
import {Pagination} from 'antd';
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

class ReplyList extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      edit: false,
      replyId: '',
      receiveUser:'',//被回复人
      passRichValue: '', 
      pageNum: 1,
      pageSize: 5
    };
  }
  
  changePageNum = () => { //子回复删除,底部分页器中pageNum变化
    const { list } = this.props;
    const { pageSize } = this.state;
    let total = list.length;
    if(total - 1 <= pageSize ) {
      this.setState({ pageNum: 1});
    } else if( (total - 1 > pageSize) && (list.length - 1 % 5 == 0)) {
      this.setState( { pageNum: pageNum-- });
    }
  }
  reply = (id, receiveUser,receiveUserName,resiveUserType, type) => {
    let passRichValue = getLabel('385534', "回复") + receiveUserName;
    this.setState({
      edit: true,
      replyId: id,
      receiveUser : receiveUser,
      resiveUserType: resiveUserType,
      passRichValue: passRichValue,
    });
  }
  
  submit = (params, callback) => {
    let saveParams = {...params, replyType: 'comment'};
    saveParams.replayid = this.state.replyId;
    saveParams.receiveUser = this.state.receiveUser;
    saveParams.resiveUserType = this.state.resiveUserType;
    saveParams.contentId = this.props.contentId;
    isFunction(this.props.submit) 
    && this.props.submit(saveParams, this.cancel.bind(this, callback));
  }

  cancel = (callback) => {
    this.setState({
      edit: false,
      replyId: '',
      receiverUser:''
    },()=>{
      this.refs.richText 
      && this.refs.richText.wrappedInstance 
      && this.refs.richText.wrappedInstance.clear();
    });
    isFunction(callback) && callback();
  }
  
  changePage = (pageNum) => {
    this.setState({pageNum});
  }

  render() {
    const {list, canReply, logParams, logid,isprint = '0', isFromPortal = false, hasSubmitRight = false } = this.props;
    const { passRichValue } = this.state; 
    let {edit, pageNum, pageSize} = this.state, total = list.length;
    let showList = [];
    let endNum = pageNum * pageSize;
    if (endNum > total) {
      endNum = total;
    }
    for (let i = (pageNum - 1) * pageSize; i < endNum; i++) {
      showList.push(list[i]);
    }
    return (
      <div className="wea-cowork-reply-wrap">
        {
          showList.map((item,ecindex) => {
            return (
              <ReplyItem ecId={`${this && this.props && this.props.ecId || ''}_ReplyItem@nb8pvr@${ecindex}`} 
                operate={this.props.operate} 
                changePageNum= {this.changePageNum} 
                replyInfo={item}
                logid={logid} 
                reply={this.reply}
                isprint={isprint}
                isFromPortal={isFromPortal}
                hasSubmitRight={hasSubmitRight}
              />
            )
          })
        }
        {
          (total > pageSize) 
            && <div className="small-page">
                  <Pagination ecId={`${this && this.props && this.props.ecId || ''}_Pagination@yt4m6c`} 
                    size="small" 
                    total={total} 
                    pageSize={pageSize} 
                    onChange={this.changePage} 
                    current={pageNum}
                  />
                  <div className="clear-both"/>
              </div>
          }
        {
          edit 
            && <CommunicationRichText ecId={`${this && this.props && this.props.ecId || ''}_CommunicationRichText@bxfu4h`} 
                hasSubmit={true} 
                ref="richText" 
                submit={this.submit} 
                cancel={this.cancel} 
                logParams={logParams}
                appStatus={this.props.appStatus}  
                editType="comment" 
                placeholder={passRichValue} 
                type={'reply'}
            />
        }
      </div>
    );
  }
}

export default ReplyList;